
package com.vodafone.nowplus.android.peoplemail.singlesignon;

import java.security.cert.X509Certificate;

import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.util.Log;


public class SSOReceiver extends Activity {

  ////  public static DatabaseHelper mDBHelper;

    public static AssetManager assetManager;

    public static final String certificateKeyName = "certificate";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    //    mDBHelper = ((MainApplication)getApplication()).getDatabase();
        assetManager = getAssets();
    }

    private UserData getUserData() {
        UserData userData = new UserData();
        UserDataReader userDataReader = UserDataReader.getInstance();
        userDataReader.populateUserData(userData);
        return userData;
    }

 @Override
    protected void onResume() {
        super.onResume();
        Intent callingIntent = getIntent();
        X509Certificate certificate =(X509Certificate) callingIntent.getSerializableExtra (certificateKeyName); 
        String callingPackage=getCallingPackage();
        if (callingIntent== null || callingPackage==null){
            setResult(RESULT_CANCELED);
            finish();
            return;
        }

        if (certificate != null
                && new IdentityVerifier ().verifyIdentity (certificate,callingPackage)){
            UserData userData = getUserData();
            Intent intent = new Intent();
            try{
                userData.enCryptUserData(certificate.getPublicKey());
                userData.writeToIntent(intent);

            }catch (Exception e){
                Log.e("SSOReceiver",e.toString());
                finish();
                return;
            }
            setResult(RESULT_OK, intent);
        } else {
            setResult(RESULT_CANCELED);

        }

        finish();

    }

}
